package com.cskaoyan.market.admin.service.impl;

import com.cskaoyan.market.admin.service.MarketLogService;
import com.cskaoyan.market.db.domain.MarketLog;
import com.cskaoyan.market.db.domain.MarketLogExample;
import com.cskaoyan.market.db.mapper.MarketLogMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class MarketLogServiceImpl implements MarketLogService {
    @Override
    public List<MarketLog> getLogs(String page, String limit, String sort, String order, String name) {
        //创建映射关系和session
        SqlSession session = MyBatisUtil.getSession();
        MarketLogMapper logMapper = session.getMapper(MarketLogMapper.class);
        //创建查询对象和查询条件
        MarketLogExample logExample = new MarketLogExample();
        MarketLogExample.Criteria criteria = logExample.createCriteria();
        String values = "%" + name + "%";
        if (!StringUtils.isEmpty(name)) {
            criteria.andAdminLike(values);
        }
        //设定排序规则
        logExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
        //把获取出来的数据返回给前端响应
        List<MarketLog> marketLogs = logMapper.selectByExample(logExample);
        //事务的关闭
        session.commit();
        session.close();
        return marketLogs;
    }
}

